/**
 *
 */
package com.angel.io.type.rows.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import com.angel.common.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowValueAccessException;
import com.angel.io.separator.ColumnSeparator;
import com.angel.io.type.rows.ExportRowFile;
import com.angel.io.type.rows.ImportRowFile;

/**
 * @author William
 *
 */
public class PlainRowFile implements ImportRowFile, ExportRowFile{

	private ColumnSeparator separator;

	private String[] cols;

	public PlainRowFile(String row){
		this(row, ColumnSeparator.COMMA);
	}

	public PlainRowFile(ColumnSeparator columnSeparator, Integer quanityColumns){
		super();
		this.separator = columnSeparator;
		this.cols = new String[quanityColumns.intValue()];
	}

	public PlainRowFile(String row, ColumnSeparator columnSeparator){
		super();
		this.separator = columnSeparator;
		this.cols = row.split(separator.getValue());
	}

	public Float getFloatValue(int position) {
		this.checkPosition(position);
		return Float.valueOf(this.cols[position]);
	}

	public void checkPosition(int position){
		if((this.cols != null && this.cols.length < position) || (this.cols == null)){
			throw new InvalidRowValueAccessException("Invalid access to position [" + position + "]. Row contains [" + ((this.cols != null) ? this.cols.length: 0)+ "] positions.");
		}
	}

	public Float[] getFloatValues() {
		Float[] floatsValues = new Float[this.cols.length];
		for(int i = 0; i < this.getRows().length; i++){
			floatsValues[i] = Float.valueOf(this.cols[i]);
		}
		return floatsValues;
	}

	public Integer getIntegerValue(int position) {
		this.checkPosition(position);
		return Integer.valueOf(this.cols[position]);
	}

	public Integer[] getIntegerValues() {
		Integer[] integersValues = new Integer[this.cols.length];
		for(int i = 0; i < this.getRows().length; i++){
			integersValues[i] = Integer.valueOf(this.cols[i]);
		}
		return integersValues;
	}

	public String getStringValue(int position) {
		this.checkPosition(position);
		return this.cols[position];
	}

	public String[] getStringValues() {
		return this.cols;
	}

	public Object getValue(int position) {
		this.checkPosition(position);
		return this.cols[position];
	}

	public Object[] getValues() {
		Object[] objectValues = new Object[this.cols.length];
		for(int i = 0; i < this.getRows().length; i++){
			objectValues[i] = this.cols[i];
		}
		return objectValues;
	}

	/**
	 * @return the separator
	 */
	public ColumnSeparator getSeparator() {
		return separator;
	}

	/**
	 * @param separator the separator to set
	 */
	public void setSeparator(ColumnSeparator separator) {
		this.separator = separator;
	}

	/**
	 * @return the row
	 */
	public String getRow() {
		this.fillRowSeparatorWithSeparator();
		return StringHelper.convertToPlainString(this.cols);
	}

	/*
	public void setRow(String row) {
		this.row = row;
	}*/

	private void fillRowSeparatorWithSeparator() {
		int position = this.cols.length;
		for(int i = 0; i<position; i++){
			if(this.cols[i] != null){
				if(i == position - 1){
					this.cols[i] = this.cols[i];
				} else {
					this.cols[i] = this.cols[i].concat(this.getSeparator().getValue());
				}
			} else {
				this.cols[i] = this.getSeparator().getValue();
			}
		}
	}

	/**
	 * @return the rows
	 */
	public String[] getRows() {
		return this.cols;
	}

	public int quantityColumns() {
		return this.cols.length;
	}

	public Date getDateValue(DateFormat dateFormat, int position) {
		String value = this.getStringValue(position);
		try {
			return dateFormat.parse(value);
		} catch (ParseException e) {}
		return new Date();
	}


	public String toString(){
		return this.getRow();
	}

	public void addDateValue(Date value, DateFormat dateFormat, int position) {
		this.checkPosition(position);
		this.addValue(dateFormat.format(value), position);
	}

	public void addFloatValue(Float value, int position) {
		this.checkPosition(position);
		this.addValue(value, position);
	}

	public void addIntegerValue(Integer value, int position) {
		this.checkPosition(position);
		this.addValue(value.toString(), position);
	}

	public void addStringValue(String value, int position) {
		this.checkPosition(position);
		this.addValue(value.toString(), position);
	}

	public void addValue(Object value, int position) {
		this.checkPosition(position);
		this.cols[position] = value.toString();
	}


	public String export() {
		return this.toString();
	}

	public Double getDoubleValue(int position) {
		this.checkPosition(position);
		return Double.valueOf(this.cols[position]);
	}

	public Double[] getDoubleValues() {
		Double[] doubleValues = new Double[this.cols.length];
		for(int i = 0; i < this.getRows().length; i++){
			doubleValues[i] = Double.valueOf(this.cols[i]);
		}
		return doubleValues;
	}

	public void addDoubleValue(Double value, int position) {
		this.checkPosition(position);
		this.cols[position] = value.toString();
	}

	public boolean isEmpty() {
		for(String value : this.cols){
			if(!StringHelper.isEmpty(value)){
				return false;
			}
		}
		return true;
	}
}
